// pages/welcome/welcome.js

const APP_ID = 'wxcdf0cf9fcd4e0fc7'; //输入小程序appid
const APP_SECRET = '596e5dad3550a055a282c90035b2cedc'; //输入小程序app_secret
var OPEN_ID = '' //储存获取到openid
var SESSION_KEY = '' //储存获取到session_key
const app = getApp();
var r;
const infoUtils = require("../../utils/userInfo.js");
const api = require("../../api.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    openid: null,
    session_key: null
  },
  onLoad:function(options){
        r=this;
  },
  /** 暂不授权 */
  goBack: function () {
    wx.navigateBack({
      delta: 1
    })
  },
  onGotUserInfo: function(e) {
    console.log("e", e.detail.userInfo)
    if (e.detail.userInfo) {
      var wxUserInfo = e.detail.userInfo;
      wx.login({
        success: function(redd) {
          if (redd.code) {
            wx.request({
              url: api.login.getOpenId + '/' + redd.code,
              success: function (back) {
                // console.log(back);
                if (back.statusCode == 200&&back.data.code==200 ) {
                  var openId = back.data.data;
                  wxUserInfo.openId = openId;
                  app.globalData.wxUserInfo=wxUserInfo;
                  var value = wx.getStorageSync('inviteOpenId');
                  if(app.globalData.shareType){
                    if(app.globalData.shareType==0){
                      wx.reLaunch({
                        url: `/pages/index/index?id=${app.globalData.userId}&shareType=0` 
                      });
                    }
                    else if(app.globalData.shareType==1){
                      wx.reLaunch({
                        url: `/pages/index/index?id=${app.globalData.userId}&shareType=1` 
                      });
                    }
                    else if(app.globalData.shareType==2){
                      wx.reLaunch({
                        url: `/pages/index/index?id=${app.globalData.userId}&shareType=2` 
                      });
                    }
                    else if(app.globalData.shareType==3){
                      wx.reLaunch({
                        url: `/pages/discover/discover?id=${app.globalData.userId}&shareType=3` 
                      });

                    }else if(app.globalData.shareType==4){
                      wx.reLaunch({
                        url: `/pages/audio/audio?id=${app.globalData.userId}&shareType=4` 
                      });

                    }else if(app.globalData.shareType==7){
                      wx.reLaunch({
                        url: `/pages/audio/audio?id=${app.globalData.userId}&inviteOpenId=${value}&shareType=7` 
                      });
                      setTimeout(()=>{
                        wx.setStorageSync('inviteOpenId', '');
                      },500)
                    }
                    setTimeout(()=>{
                      app.globalData.shareType='';
                      app.globalData.userId='';
                     
                    },1000)
                  }else{
                    wx.navigateBack({
                      delta: 1
                    })
                  }
                }else{
                  wx.showModal({
                    title: '授权失败',
                    content: '请重新点击',
                    showCancel: false
                  })
                }
              }
            })
          }
        }
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

})